<template>
  <BasicContainer>
    <FyCurd
      ref="crud"
      v-model="form"
      :page.sync="page"
      :data="tableData"
      :permission="permissionList"
      :table-loading="tableLoading"
      :option="tableOption"
      :before-open="handleOpenBefore"
      @on-load="getList"
      @search-change="searchChange"
      @refresh-change="refreshChange"
      @size-change="sizeChange"
      @current-change="currentChange"
      @row-update="handleUpdate"
      @row-save="handleSave"
      @row-del="handleDel">
      <template #menuLeft>
        <ElButton
          v-if="permissions.problem_rectifydetailed_export"
          class="filter-item"
          plain
          type="primary"
          size="small"
          icon="el-icon-download"
          @click="exportExcel">
          导出
        </ElButton>
      </template>
      <template #rectifyNum="{ row }">
        <ElTag style="cursor: pointer" @click="rectifyNumClick(row)">{{ row.rectifyNum }}</ElTag>
      </template>
      <template #rectifyNumForm>
        <ElInput
          v-model="form.rectifyNum"
          :disabled="rectifyNumDisable"
          placeholder="选择问题单号"
          @focus="openProblemRectifyDialog()" />
      </template>
      <template #dutyUsernameForm>
        <ElInput
          v-model="form.dutyUsername"
          :disabled="false"
          placeholder="选择责任人"
          @focus="openSelectUserDialog()" />
      </template>
    </FyCurd>
    <ProblemRectify v-if="visibleRectify" ref="rectify" @getProblemRectifyData="getProblemRectifyData" />
    <ElementDialog :visible.sync="visibleRectifyForm" title="整改措施" top="5%" width="95%">
      <RectifyForm v-if="visibleRectifyForm" ref="rectifyForm" />
      <template #footer>
        <span class="dialog-footer">
          <ElButton @click="visibleRectifyForm = false">关闭</ElButton>
        </span>
      </template>
    </ElementDialog>
    <UserPicker
      v-if="visibleSelectUser"
      :pre-selected-users="users"
      :reserve-position-on-close="false"
      close-on-click-modal
      :visible.sync="visibleSelectUser"
      @confirm="getUserData" />
  </BasicContainer>
</template>

<script>
  import { fetchList, addObj, putObj, delObj } from '@/api/problem/rectifydetailed'
  import { tableOption } from '@/const/crud/problem/rectifydetailed'
  import { mapState } from 'vuex'
  import { getRectifyObj } from '@/api/problem/rectify'
  import RectifyForm from '@/module/problem/rectify/rectify-form'
  import { services } from '@/config'

  export default {
    name: 'RectifyDetailed',
    components: {
      RectifyForm
    },
    props: {
      rectifyId: {
        type: Number,
        default: -1
      },
      rectifyNum: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        searchForm: {},
        tableData: [],
        users: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption,
        visibleRectify: false,
        form: {},
        viewType: '',
        visibleSelectUser: false,
        rectifyNumDisable: false,
        visibleRectifyForm: false
      }
    },
    computed: {
      ...mapState('user', ['permissions']),
      permissionList() {
        return {
          addBtn: this.$isEmpty(this.permissions.problem_rectifydetailed_add, false),
          delBtn: this.$isEmpty(this.permissions.problem_rectifydetailed_del, false),
          editBtn: this.$isEmpty(this.permissions.problem_rectifydetailed_edit, false)
        }
      }
    },
    methods: {
      // 列表查询
      getList(page, params) {
        this.tableLoading = true
        if (this.rectifyId !== -1) {
          this.searchForm.rectifyId = this.rectifyId
        }
        fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize
            },
            params,
            this.searchForm
          )
        )
          .then(response => {
            this.tableData = response.data.data.records
            this.page.total = response.data.data.total
            this.tableLoading = false
          })
          .catch(() => {
            this.tableLoading = false
          })
      },
      // 删除
      handleDel: function (row, index) {
        this.$confirm(`是否确认删除ID为${row.id}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(data => {
            this.$message.success('删除成功')
            this.getList(this.page)
          })
      },
      handleOpenBefore(show, type) {
        window.boxType = type
        this.viewType = type
        if (this.rectifyId !== -1 && this.rectifyNum !== '') {
          this.form.rectifyId = this.rectifyId
          this.form.rectifyNum = this.rectifyNum
          this.rectifyNumDisable = true
        }
        show()
      },
      // 更新
      handleUpdate: function (row, index, done, loading) {
        putObj(row)
          .then(data => {
            this.$message.success('修改成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 保存
      handleSave: function (row, done, loading) {
        addObj(row)
          .then(data => {
            this.$message.success('添加成功')
            done()
            this.getList(this.page)
          })
          .catch(() => {
            loading()
          })
      },
      // 每页条数改变事件
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      // 当前页发生改变事件
      currentChange(current) {
        this.page.currentPage = current
      },
      // 查询事件
      searchChange(form, done) {
        this.searchForm = form
        this.page.currentPage = 1
        this.getList(this.page, form)
        done()
      },
      // 刷新事件
      refreshChange() {
        this.getList(this.page)
      },
      //  导出excel
      exportExcel() {
        const date = this.$dateFormat(new Date())
        this.$downBlobFile(
          `${services.personnelService}rectifydetailed/export`,
          this.searchForm,
          `整改措施责任人_${date}.xlsx`
        )
      },
      getProblemRectifyData(listData) {
        for (let i = 0; i < listData.length; i++) {
          this.form.rectifyId = listData[i].id
          this.form.rectifyNum = listData[i].number
        }
      },
      openProblemRectifyDialog() {
        this.visibleRectify = this.viewType !== 'view'
        this.$nextTick(() => {
          this.$refs.rectify.init()
        })
      },
      rectifyNumClick(row) {
        this.visibleRectifyForm = true
        getRectifyObj(row.rectifyId).then(response => {
          this.$refs.rectifyForm.initData(response.data.data)
        })
      },
      openSelectUserDialog() {
        this.visibleSelectUser = this.viewType !== 'view'
      },
      getUserData(users) {
        const user = users[0]
        this.form.dutyUsername = user.name
        this.form.dutyUserid = user.id
        this.form.dutyOuid = user.deptId
        this.form.dutyName = user.deptName
        this.form.dutyFullname = user.deptName
      }
    }
  }
</script>
